package com.jl.jlapp.utils;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomViewTarget;
import com.jl.jlapp.R;

/**
 * Glide 工具类
 * Google推荐的图片加载库，专注于流畅的滚动
 * <p>
 * Glide 比Picasso  加载快 但需要更大的内存来缓存
 * <p>
 * Glide 不光接受Context，还接受Activity 和 Fragment ,图片加载会和Activity/Fragment的生命周期保持一致 在onPause（）暂停加载，onResume（）恢复加载
 * <p>
 * 支持GIF格式图片加载
 */
public class GlideUtils {

    public static void loadUrlImage(Context context, String url, ImageView imageView, int placeholderId) {
        RequestOptions options = new RequestOptions().placeholder(placeholderId).error(placeholderId);
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(imageView);
    }

    public static void loadUrlImage(Context context, String url, ImageView imageView) {
        loadUrlImage(context, url, imageView,R.drawable.img_noimg_m);
    }

    public static void loadCircleUrlImage(Context context, String url, ImageView imageView, int placeholderId) {
        RequestOptions options = new RequestOptions().circleCrop().placeholder(placeholderId).error(placeholderId);
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(imageView);
    }

    public static void loadCircleUrlImage(Context context, String url, ImageView imageView) {
        loadCircleUrlImage(context, url, imageView,R.drawable.img_noimg_m);
    }

    public static void loadUrlImageForTarget(Context context, String url, CustomViewTarget target, int placeholderId) {
        RequestOptions options = new RequestOptions();
        if (placeholderId != 0) {
            options.placeholder(placeholderId).error(placeholderId);
        }

        Glide.with(context)
                .asBitmap()
                .load(url)
                .apply(options)
                .into(target);
    }

    public static void loadUrlImageForTarget(Context context, String url, CustomViewTarget target) {
        loadUrlImageForTarget(context, url, target, R.drawable.img_noimg_m);
    }
}
